import Vue from 'vue'
import $ from '@/common'
export default {
  name: 'rm',
  state: {
    // 编码 Subject目标对象 Owned已选资源 Resource资源
    // 选中数据的id
    rmModel: {},
    ownedCount: 0,
    modelLoad: false,
    path: {
      ds: '/service/{app}/dataset/model/{code}',
      model: '/service/{app}/resource_mapping/model/{code}',
      subject: '/service/{app}/resource_mapping/subject/data/{code}/page',
      resource: '/service/{app}/resource_mapping/resource/data/{code}/page?subject_id={subject_id}&enable_choiced=false',
      owned: '/service/{app}/resource_mapping/resource/data/{code}/page?subject_id={subject_id}&enable_choiced=true',
      // put
      add: '/service/{app}/resource_mapping/resource/data/{code}?subject_id={subject_id}',
      // delete
      remove: '/service/{app}/resource_mapping/resource/data/{code}?subject_id={subject_id}',
      // 排序
      sort: '/service/{app}/resource_mapping/resource/data/sort/{code}?subject_id={subject_id}'
    },
    selectedSubject: null,
    // 状态变化标记，当资源发生变化时切换页签可根据该值判断是否刷新数据
    inChange: false
  },
  actions: {
    updateModel({state, dispatch, commit},model) {
      commit('setModel', model)
      // 设置主体查询路径，并刷新查询数据
      commit('subject/setPath', $.composePath(state.path.subject, {code: model.code}))
      commit('resource/setPath', $.composePath(state.path.resource, {code: model.code}))
      dispatch('subject/fetch')
    },
    subjectSelect({dispatch, commit, state},{data, index}) {
      // 临时设置
      commit('subjectChange', data.id)
      commit('owned/setPath', $.composePath(state.path.owned, {
        code: state.rmModel.code,
        subject_id: data.id
      }))
      commit('resource/setPath', $.composePath(state.path.resource, {
        code: state.rmModel.code,
        subject_id: data.id
      }))
      dispatch('fetchOwned')
    },
    fetchSubject({dispatch}) {
      return dispatch('subject/fetch').then(() => {
        dispatch('resource/clear')
      })
    },
    fetchResource({dispatch}) {
      return dispatch('resource/fetch')
    },
    fetchOwned({dispatch, commit, state}) {
      return dispatch('owned/fetch').then(() => {
        commit('setOwnedCount', Math.max(state.owned.listData.length , state.owned.totalCount))
      })
    },
    addItem({dispatch, commit, state}, id) {
      return $.link(state.path.add, [id], {
        method: 'put',
        param: {
          subject_id: state.selectedSubject,
          code: state.rmModel.code
      }}).then((res) => {
        dispatch('fetchResource')
        commit('mappingChange', true)
        commit('setOwnedCount', state.ownedCount + 1)
      })
    },
    removeItem({dispatch, commit, state}, id) {
      return $.link(state.path.add, [id], {
        method: 'delete',
        param: {
          subject_id: state.selectedSubject,
          code: state.rmModel.code
      }}).then((res) => {
        dispatch('fetchOwned')
        commit('mappingChange', true)
      })
    },
    saveSort({state}) {
      var data = $.copyJson(state.owned.listData)
      return $.link(state.path.sort, $.setSerial(data), {
        param: {
          subject_id: state.selectedSubject,
          code: state.rmModel.code
        }
      })
    }
  },
  mutations: {
    mappingChange(state, v) {
      state.inChange = v
    },
    setModel(state, data) {
      Vue.set(state, 'rmModel', data)
    },
    subjectChange(state, id) {
      state.selectedSubject = id
    },
    setOwnedCount(state, v) {
      state.ownedCount = v
    }
  }
}
